package com.glela.dv.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.glela.common.util.ExtendUtil;
import com.glela.dv.beans.PageBean;
import com.glela.dv.beans.SearchBean;
import com.glela.dv.dao.IKeepDao;
import com.glela.dv.service.IKeepService;
import com.glela.dv.vo.KeepVo;

/**
 * @author liujie
 * @date 2016年4月11日
 *
 */
@Service("keepService")
public class KeepService implements IKeepService {

	@Autowired
	IKeepDao keepDao;

	@Override
	public List<KeepVo> queryKeepByCondition(PageBean pageBean) {
		PageHelper.startPage(pageBean.getPage(), pageBean.getRows());
		return keepDao.queryKeepByCondition(pageBean);
	}

	@Override
	public List<Map<String, Object>> queryKeepChartByCondition(SearchBean searchBean) {
		List<KeepVo> keepList=keepDao.queryKeepByCondition(searchBean);
		List<Map<String, Object>> seriesList = new ArrayList<Map<String, Object>>();
		Map<String, Object> seriesMap = new HashMap<String, Object>();
		List<Double> seriesDataList = new ArrayList<Double>();
		Date preRegActDate=ExtendUtil.strDateTimeToDate(searchBean.getStartDate());
		int cnt=0;
		if(keepList!=null)
		{
			for (KeepVo keepVo : keepList) {
				if(preRegActDate!=null)
				{
					int days=ExtendUtil.mathTwoDate(preRegActDate, keepVo.getRegActDate());
					if(cnt==0&&days>0)
					{
						days++;
					}
					for(int i=1;i<days;i++)
					{
						seriesDataList.add(null);
					}
				}
				Double data=null;
				switch (searchBean.getIndex().intValue()) {
				case 0:
					data=keepVo.getSurvivalRate2();
					break;
				case 1:
					data=keepVo.getSurvivalRate7();
					break;
				case 2:
					data=keepVo.getSurvivalRate14();
					break;
				case 3:
					data=keepVo.getSurvivalRate30();
					break;
				default:
					break;
				}
				if(data.doubleValue()==-100)
				{
					data=null;
				}
				seriesDataList.add(data);
				preRegActDate=keepVo.getRegActDate();
				cnt++;
			}
		}
		seriesMap.put("name", "留存率");
		seriesMap.put("data", seriesDataList);
		seriesList.add(seriesMap);
		return seriesList;
	}

}
